<?php
/**
 * @name Dispatch
 *
 * LICENSE
 *
 * This source file is subject to the BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.phpxe.com/license/
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to Chaodong.Sheng <scd113@gmail.com> so we can send you a copy immediately.
 *
 * @category   Controller of Scd Framework
 * @package    Scd
 * @copyright  Copyright (c) 2007 PHPXe Web Studio <http://www.phpxe.com>
 * @license    http://framework.phpxe.com/license/
 * @version    $Id: Dispatch.php Wed Sep 26 00:54:37 CST 2007 Davy $
 */
include_once("Scd/Controller/Exception.php");
include_once("Scd/Controller/Dispatch/Abstract.php");
class Scd_Controller_Dispatch extends Scd_Controller_Dispath_Abstract {
	var $_confIns;//过渡参数句柄；为setTemplate
	
	function Scd_Controller_Dispatch($configIns)
	{
		/**
		 * 为其它可能做扩展，不然就直接赋theme值
		 */
		$this->_confIns = $configIns;
		//拒绝直接dispath configIns
		//parent::dispatch($configIns);
		parent::_config($this->_confIns);
		$this->dispatch();
	}
	
	/**
	 * 准备由子类来承担抽象类的dispatch具体实施情况。
	 */
	function dispatch()
	{
		/**
		 * 从抽象类取得系列相关重要的参数
		 *   ["_curAction"] => string(4) "view"
		 *   ["_curController"] => string(5) "index"
		 *   ["_curModule"] => string(7) "content"
		 *   ["_invokeParams"] => array(1) {
		 *     ["id"] => string(2) "11"
		 *   }
		 *   ["_curCtrlDir"] => string(30) "application\content\controller"
		 *   ["_curActionName"] => string(10) "viewAction"
		 *   ["_curCtrlName"] => string(15) "IndexController"
		 */
		parent::dispatch();
		/**
         * Load the controller class file
         * 获得当前controller
         */
		$className		= $this->loadClass($this->_curCtrlName);

		/**
         * Instantiate controller with request, response, and invocation
         * arguments; throw exception if it's not an action controller
         * 没有明白参数是干吗用的,删掉
         */
		//$controller = new $className($request, $this->getResponse(), $this->getParams());
		$controller		= new $className($this);
		if (!is_a( $controller, Scd_Controller_Action )) {
			return  new Scd_Controller_Exception("Controller '$className' is not an instance of Scd_Controller_Action");
		}
		if (method_exists($controller, $className)) {
			//Application的xxxxxController中如果存在“构造体”那么就需要执行父类的构造体
			//以满足view,pdo赋值
			//不知道为什么子类没有构造体反而能自动执行父类的构造体。
			//PHP5中不需要执行Scd_Controller_Action？？？？
			$controller->Scd_Controller_Action($this);
		}

		/**
         * Retrieve the action name
         * 重获action 名称
         */
		if(false == method_exists($controller, $this->_curActionName)){
			//不存在输出error函数失败
			//$this->getErrorControllerClass();
			//$this->_curActionName = $this->formatActionName($this->_errAction);
			$this->_curActionName = $this->formatActionName($this->_defaultAction);
		}
		
		$action	= $this->_curActionName;//判断函数的方法是否存在
		
		/**
         * Dispatch the method call
         * 直接执行action传入参数,
         * 这地方我想直接加入smarty模版设置
         */
		if (true == $this->_confIns->smarty) {
			//复写template_dir
			$controller->view->setTemplateDir($this->_confIns->theme ."/". $this->_curModule);
		}
		$controller->$action( $this->_invokeParams );
		// Destroy the page controller instance and reflection objects
		$this->destory();
		$controller = null;
	}
}